﻿using GNSystem.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using WebMatrix.WebData;

namespace GNSystem.Helpers
{
    public class Forum
    {
        struct PostButtons
        {
            public const string EditButton = @"<a href=""/Post/Edit/{0}"">Edit</a>";
            public const string ReportButton = @"<a href=""/Post/Report/{0}"">Report</a>";
            public const string DeleteButton = @"<a href=""/Post/Delete/{0}"">Delete</a>";
        }
        struct ThreadButtons
        {
            public const string EditButton = @"<a href=""/Thread/Edit/{0}"">Edit</a>";
            public const string ReportButton = @"<a href=""/Thread/Report/{0}"">Report</a>";
            public const string DeleteButton = @"<a href=""/Thread/Delete/{0}"">Delete</a>";
        }
        const string PostTemplate = @"<table>
                <tr>
                    <td rowspan=""3"">
                        {0}
                    </td>
                    <td><code>{1}</code></td>
                </tr>
                <tr>
                    <td>{2}</td>
                </tr>
                <tr>
                    <td>
                        {3}
                    </td>
                </tr>
            </table>";
        public static MvcHtmlString Post(Post post, DataContext db, int uid)
        {
            bool isMod, isOwner;
            isMod = db.UserGroups.Find(db.UserProfiles.Find(uid).GroupID).IsStaff;
            isOwner = uid == post.UserID;
            var str = new MvcHtmlString(string.Format(PostTemplate, User.UserName(post.UserID, db), post.CreationTime, post.Content, GetAvailablePostButtons(isMod, isOwner, post.ID)));
            return str;
        }
        public static MvcHtmlString Post(int pid, DataContext db, int uid)
        {
            return Post(db.Posts.Find(pid), db, uid);
        }
        public static MvcHtmlString Thread(Thread thread, DataContext db, int uid)
        {
            bool isMod, isOwner;
            isMod = db.UserGroups.Find(db.UserProfiles.Find(uid).GroupID).IsStaff;
            isOwner = uid == thread.UserID;
            
            return new MvcHtmlString(
                string.Format(PostTemplate, User.UserName(thread.UserID, db),
                thread.CreationTime, thread.Content, 
                GetAvailableThreadButtons(isMod, isOwner, thread.ID)));;
        }
        public static MvcHtmlString Thread(int tid, DataContext db, int uid)
        {
            return Thread(db.Threads.Find(tid), db, uid);
        }

        private static MvcHtmlString GetAvailablePostButtons(bool isMod, bool isOwner, int pid)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat(PostButtons.ReportButton, pid);
            if (isOwner || isMod)
            {
                sb.AppendFormat(PostButtons.EditButton, pid);
            }
            if (isMod)
            {
                sb.AppendFormat(PostButtons.DeleteButton, pid);
            }
            return new MvcHtmlString(sb.ToString());
        }
        private static MvcHtmlString GetAvailableThreadButtons(bool isMod, bool isOwner, int tid)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat(ThreadButtons.ReportButton, tid);
            if (isOwner || isMod)
            {
                sb.AppendFormat(ThreadButtons.EditButton, tid);
            }
            if (isMod)
            {
                sb.AppendFormat(ThreadButtons.DeleteButton, tid);
            }
            return new MvcHtmlString(sb.ToString());
        }
    }
}